package game.zqll.systems;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.systems.IteratingSystem;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import game.zqll.components.PosComp;
import game.zqll.components.SizeComp;
import game.zqll.components.TextureRegionComp;

public class TextureRegionRenderSystem extends IteratingSystem {

    private CameraSystem cameraSystem;
    private final SpriteBatch spriteBatch;
    private ComponentMapper<TextureRegionComp> textureRegion;
    private ComponentMapper<PosComp> pos;
    private ComponentMapper<SizeComp> size;

    public TextureRegionRenderSystem() {
        super(Aspect.all(TextureRegionComp.class, PosComp.class, SizeComp.class));
        this.spriteBatch = new SpriteBatch();
    }

    @Override
    protected void process(int entityId) {
        TextureRegionComp textureRegionComp = textureRegion.get(entityId);
        if (textureRegionComp.region == null || textureRegionComp.region.getTexture() == null) {
            return;
        }
        PosComp posComp = pos.get(entityId);
        SizeComp sizeComp = size.get(entityId);
        spriteBatch.begin();
        spriteBatch.setProjectionMatrix(cameraSystem.getCamera().combined);
        spriteBatch.draw(textureRegionComp.region, posComp.x, posComp.y, sizeComp.width, sizeComp.height);
        spriteBatch.end();
    }
}
